<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>找回密码</title>
    <script src="js/jquery-1.11.3.js"></script>
    <link href="css/findpassword.css" rel="stylesheet" type="text/css" />
  </head>

  <body>
    <div class="main_div">
      <div class="title">找回密码</div>
      <div class="input_text_div">
        <i class="username"></i>
        <input type="text" id="UserName" placeholder="账号" />
      </div>
      <div class="input_text_div">
        <i class="userpassword"></i>
        <input type="text" id="UserPassword" placeholder="新密码" />
      </div>
      <div class="code_div">
        <i class="code"></i>
        <input type="text" id="code" placeholder="验证码" />
        <a class="codeA" onclick="sendCode(this)">发送验证码</a>
      </div>
      <div class="butten_div">
        <a onclick="updatePassword()">更新密码</a>
        <i>想起密码了?<a onclick="login()">点击此处</a>登录</i>
      </div>
    </div>
    <script>
      function sendCode(btn) {
        var username = $("#UserName").val();
        if (username == null || username.trim() == "") {
          alert("用户名不能为空！");
          return;
        }
        btnDisTime(btn);
        $.ajax({
          type: "POST",
          url: "http://localhost:8080/user/sendEmailCode",
          data: {
            username: username,
          },
          success: function (data) {
            if (data == 1) {
            } else if (data == -1) {
              alert("账号不存在！");
              removedisabledBtn(btn);
              $(btn).html("发送验证码");
              //关闭定时器
              clearTimeout(mytimeout);
            } else if (data == -2) {
              // alert("邮件发送失败！");
            }
          },
        });
      }

      var time = 60;
      var mytimeout = null;
      function btnDisTime(btn) {
        disabledBtn(btn);
        $(btn).html(time + "S");

        mytimeout = setTimeout(function () {
          time--;

          if (time > 0) {
            btnDisTime(btn);
          } else {
            removedisabledBtn(btn);
            $(btn).html("发送验证码");
            time = 60;
          }
        }, 1000);
      }

      function disabledBtn(btn) {
        // $(btn).addClass("isNot");
        $(btn).css("background-color", "#c5c5c5");
        $(btn).attr("onclick", "");
      }
      function removedisabledBtn(btn) {
        $(btn).css("background-color", "");
        $(btn).attr("onclick", "sendCode(this)");
      }
      function updatePassword() {
        var username = $("#UserName").val();
        var newpassword = $("#UserPassword").val();
        var code = $("#code").val();

        $.ajax({
          type: "POST",
          url: "http://localhost:8080/user/updatePassword",
          data: {
            username: username,
            newpassword: newpassword,
            code: code,
          },
          success: function (data) {
            if (data == 1) {
              alert("密码重置成功");
              location.href = "login.html";
            } else if (data == -1) {
              alert("无效或已过期的验证码");
            } else if (data == -2) {
              alert("验证码错误");
            }
          },
        });
      }
      function login() {
        location.href = "login.html";
      }
    </script>
  </body>
</html>
